<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
              <el-form-item label="单据编号:">
                <el-input
                  clearable
                  v-model="where.danjNo"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :md="6" :sm="12">
            <el-form-item label="起始日期:">
                <el-date-picker v-model="where.riqiBegin" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12">
            <el-form-item label="结束日期:">
                <el-date-picker v-model="where.riqiEnd" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
            <el-col :lg="6" :md="12">
              <el-form-item label="单位名称:">
                <el-input
                  clearable
                  v-model="where.danwName"
                  placeholder=""/>
              </el-form-item>
            </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :lg="5" :md="12">
              <el-form-item label="商品编号:">
                <el-input
                  clearable
                  v-model="where.shangpNo"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="5" :md="12">
              <el-form-item label="批号:">
                <el-input
                  clearable
                  v-model="where.lot"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="5" :md="12">
              <el-form-item label="入库单号:">
                <el-input
                  clearable
                  v-model="where.rukNo"
                  placeholder=""/>
              </el-form-item>
            </el-col>
          <el-col :lg="5" :md="12">
              <el-form-item label="状态:">
                <el-select
                clearable
                v-model="where.zt"
                placeholder="请选择状态"
                class="ele-fluid">
                <el-option label="未同步" value="Y"/>
                <el-option label="已同步" value="N"/>
                <el-option label="E" value="E"/>
                <el-option label="H" value="H"/>
                <el-option label="ZT" value="ZT"/>
              </el-select>
              </el-form-item>
            </el-col>

          <el-col :lg="4" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                size="small"
                @click="reload">查询
              </el-button>
              <el-button @click="reset" size="small">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 275px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar" >
          <el-button v-if="false"
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            >NC回写
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="false"
            >删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-upload2"
            @click="openEdit(row)"
            >NC回写
          </el-link>
            <el-link
              type="success"
              slot="reference"
              :underline="false"
              icon="el-icon-document"
              @click="openLog(row)"
              >日志
            </el-link>
            <el-link
            type="primary"
            :underline="false"
            icon="el-icon-search"
            @click="openDetail(row)"
            >单据明细
          </el-link>

        </template>
      </ele-pro-table>
    </el-card>
    <LogList
      :dataItem="current"
      :visible.sync="showLog"
      />
      <BillDetail
      :dataItem="current"
      :visible.sync="showDetail"
      />

  </div>
</template>

<script>
import { mapGetters } from "vuex";
import LogList from './loglist.vue';
import BillDetail from './billdetail.vue';
export default {
  name: 'ExamC37InfRkScBillNew',
  components: {LogList,BillDetail},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/c37infrkscbillnew/list',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'danjNo',
          label: '订单编号',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center'
        },
        {
          prop: 'rukNo',
          label: '入库单据编号',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
        },
        {
          prop: 'ztmc',
          label: '同步状态',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'riqiChar',
          label: '入库日期',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'danwName',
          label: '供应商名称',
          showOverflowTooltip: true,
          minWidth: 220,
          align: 'center'
        },
        {
          prop: 'xierTime',
          label: '写入时间',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
        },
        // {
        //   prop: 'caigouStaff',
        //   label: '业务员',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'shouhStaff',
        //   label: '收货员',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'hanghao',
        //   label: '行号',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'shangpNo',
        //   label: '商品编号',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'lot',
        //   label: '批号',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'shengcChar',
        //   label: '生产日期',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'youxChar',
        //   label: '有效期',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'rukType',
        //   label: '入库类型',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'jihNum',
        //   label: '计划数量',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'num',
        //   label: '实际数量',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'price',
        //   label: '价格',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'hanghCgd',
        //   label: '采购单行号',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'zhijStaff',
        //   label: '质检员',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'pingxNo',
        //   label: '拼箱号',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'yansRlt',
        //   label: '验收评定',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'khZzbm',
        //   label: 'NC组织编码',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        // {
        //   prop: 'yezId',
        //   label: '九州通业主编码',
        //   showOverflowTooltip: true,
        //   minWidth: 140,
        //   align: 'center'
        // },
        {
          columnKey: 'action',
          label: '操作',
          width: 300,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }

      ],
      // 表格搜索条件
      where: {riqiBegin:this.getFromDate(),riqiEnd:new Date(),rukType:'采购入库'},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showLog: false,
      showDetail:false,
    };
  },
  methods: {
    getFromDate() {
      const timeOne = new Date()
      const year = timeOne.getFullYear()
      let month = timeOne.getMonth()
      let day = 1
      //month = month < 10 ? '0' + month : month
      //day = day < 10 ? '0' + day : day
      ///const NOW_MONTHS_AGO = `${year}-${month}-${day}`
      return new Date(year,month,day);
    },

    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {riqiBegin:this.getFromDate(),riqiEnd:new Date(),rukType:'采购入库'};
      this.reload();
    },

    openLog(row){
      console.log(row);
      this.current = row;
      this.showLog = true;
    },

    openDetail(row){
      console.log(row);
      this.current = row;
      this.showDetail = true;
    },
    /* 显示编辑 */
    openEdit(row) {
      // if (!this.selection.length) {
      //   this.$message.error('请至少选择一条数据');
      //   return;
      // }
      this.$confirm('确定要将选中的采购入库记录回写至NC吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/esbservice/cgrk/'+ [row.rukNo]).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            //this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/c37infrkscbillnew/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的采购入库/销售退回上传单表吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/c37infrkscbillnew/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped>
.ele-form-search{
  margin-top: -15px ;
  margin-bottom: -5px;
}
.el-form-item {
  margin-bottom: -5px !important;
}
</style>
